package com.pybox.util;

import com.pybox.oauth.conf.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public final class UserUtils {

    public static UserDto getCurrentUser() {
        UserDto user = null;
        if (SecurityContextHolder.getContext().getAuthentication() != null) {
            Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//            System.out.println("获取保存的对象"+obj);
            if (obj instanceof UserDto) {
                user = (UserDto) obj;
//                System.out.println(" ");
//                System.out.println("customerId："+user.getCustomerId());
//                System.out.println("Id："+user.getId());
//                System.out.println("realName："+user.getRealname());
            } else if (obj instanceof String) {
//                System.out.println("获取用户调用这个方法说说明obj----没有--userdto");
                user = new UserDto();
            } else {
                user = new UserDto();
            }
        }
        return user;
    }

    public static Integer getUserId() {
        UserDto user = getCurrentUser();
        if (null != user) {
            return user.getId();
        }
        return null;
    }

    public static Integer getCustomerId() {
        UserDto user = getCurrentUser();
        if (null != user) {
            return user.getCustomerId();
        }
        return null;
    }
    public static Integer getIsAdmin() {
        UserDto user = getCurrentUser();
        if (null != user) {
            return user.getIsAdmin();
        }
        return null;
    }


}